<?
    abstract class Principio_Model_Company_Abstract implements PrincipioLab_IDAble
    {
        use PrincipioLab_TCollection;

        protected $type;
        private $caption;
        private $proxy;
        private $vlan;
        private $vlan_pc;
        private $default_gw;
        private $netname;
        private $netmask;
        private $broadcast;
        private $range_from;
        private $range_to;
        private $ip;
        private $template_global;
        private $template_ext;
        private $id;

        const TYPE_COMPANY = 1;
        const TYPE_POOL = 2;
        const TYPE_COMPANY_IN_POOL = 3;


        public function __construct(array $values = null)
        {
            if (is_null($values))
            {
                $this->id = -1;
                $this->caption = "";
                $this->proxy = "";
                $this->vlan = -1;
                $this->vlan_pc = -1;
                $this->default_gw = "";
                $this->netname = "";
                $this->netmask = "";
                $this->broadcast = "";
                $this->range_from = "";
                $this->range_to = "";
                $this->ip = "";
                $this->template_global = "";
                $this->template_ext = "";
            }
            elseif (is_array($values))
            {
                $this->id = intval($values['id']);
                $this->caption = $values['caption'];
                $this->proxy = $values['proxy'];
                $this->vlan = intval($values['vlan']);
                $this->vlan_pc = intval($values['vlan_pc']);
                $this->default_gw = $values['default_gw'];
                $this->netname = $values['netname'];
                $this->netmask = $values['netmask'];
                $this->broadcast = $values['broadcast'];
                $this->range_from = $values['range_from'];
                $this->range_to = $values['range_to'];
                $this->ip = $values['ip'];
                $this->template_global = $values['global_template'];
                $this->template_ext = $values['ext_template'];
            }
            else
                throw new RuntimeException("Wrong parameter [values].");

        }


        public function _getID() { return $this->getID(); }


        public static function fromArray(array $values)
        {
            switch (intval($values['type']))
            {
                case self::TYPE_COMPANY:
                    return new Principio_Model_Company($values);
                    break;

                case self::TYPE_COMPANY_IN_POOL:
                    return new Principio_Model_CompanyInPool($values);
                    break;

                case self::TYPE_POOL:
                    return new Principio_Model_Pool($values);
                    break;
            };
        }


        public static function constructFromType($type)
        {
            switch (intval($type))
            {
                case self::TYPE_COMPANY:
                    return new Principio_Model_Company();
                    break;

                case self::TYPE_COMPANY_IN_POOL:
                    return new Principio_Model_CompanyInPool();
                    break;

                case self::TYPE_POOL:
                    return new Principio_Model_Pool();
                    break;
            };
        }


        public function getID()              { return $this->id; }
        public function getType()            { return $this->type; }
        public function getCaption()         { return $this->caption; }
        public function getProxy()           { return $this->proxy; }
        public function getGlobalTemplate()  { return $this->template_global; }
        public function getExtTemplate()     { return $this->template_ext; }
        public function getVlanPcID()        { return $this->vlan_pc; }
        public function getVlanID()          { return $this->vlan; }
        public function getDefaultGw()       { return $this->default_gw; }
        public function getNetmask()         { return $this->netmask; }
        public function getNetname()         { return $this->netname; }
        public function getBroadcast()       { return $this->broadcast; }
        public function getRangeTo()         { return $this->range_to; }
        public function getRangeFrom()       { return $this->range_from; }
        public function getIP()              { return $this->ip; }

        abstract public function isPool();
        abstract public function isCompanyInPool();
        abstract public function isCompany();


        public function setCaption($caption)
        {
            $this->caption = $caption;
            return $this;
        }


        public function setProxy($proxy)
        {
            $this->proxy = $proxy;
            return $this;
        }


        public function setGlobalTemplate($template_global)
        {
            $this->template_global = $template_global;
            return $this;
        }


        public function setExtTemplate($template_ext)
        {
            $this->template_ext = $template_ext;
            return $this;
        }


        public function setVlanID($vlan_id)
        {
            $this->vlan = intval($vlan_id);
            return $this;
        }


        public function setVlanPCID($vlan_pc_id)
        {
            $this->vlan_pc = intval($vlan_pc_id);
            return $this;
        }


        public function setDefaultGW($default_gw)
        {
            $this->default_gw = $default_gw;
            return $this;
        }


        public function setNetmask($netmask)
        {
            $this->netmask = $netmask;
            return $this;
        }


        public function setNetname($netname)
        {
            $this->netname = $netname;
            return $this;
        }


        public function setBroadcast($broadcast)
        {
            $this->broadcast = $broadcast;
            return $this;
        }


        public function setRangeFrom($range_from)
        {
            $this->range_from = $range_from;
            return $this;
        }


        public function setRangeTo($range_to)
        {
            $this->range_to = $range_to;
            return $this;
        }


        public function setIP($ip)
        {
            $this->ip = $ip;
            return $this;
        }


        public function setID($id)
        {
            $this->id = intval($id);
            return $this;
        }


        /*
         * Delete
         */
        public function delete($use_transaction = true) { return Principio_Model_CompanyMapper::delete($this, $use_transaction); }


        /*
         * Type In Company
         */
        public function getModelTypeInCompany(Principio_Model_Type $model_type)
        {
            foreach ($this->getTypeInCompanyCollection() as $model_type_in_company)
            {
                if ($model_type_in_company->getModelType()->getID() == $model_type->getID())
                    return $model_type_in_company;
            };

            return false;
        }


        public function getTypeInCompanyCollection()
        {
            return Principio_Model_TypeInCompanyMapper::fetchByModelCompany($this);
        }


        /*
         * Accounts
         */
        public function createAccount() { return new Principio_Model_Account($this); }

        public function getAccountCollection()
        {
            return Principio_Model_AccountMapper::fetchInCompany($this);
        }



        /*
         * SPA
         */
        public function createSPA(Principio_Model_Type $model_type)
        {
            return new Principio_Model_Spa($model_type, $this);
        }

        public function add() { return Principio_Model_CompanyMapper::save($this); }
        public function save() { return Principio_Model_CompanyMapper::save($this); }


        public function updateTemplates() { return Principio_Model_CompanyMapper::updateTemplates($this); }
    }
?>